package android.itv.controller;

import java.util.ArrayList;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.itv.R;
import android.itv.library.Database;
import android.itv.library.NotificationBar;
import android.itv.model.INotification;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class NotificationController implements INotification
{
    NotificationManager nm;
    Notification notify;
    private Context context;
    TextView textView;
    
    public NotificationController()
    {
    }
    
    public NotificationController(Context context)
    {
	this.context=context;
    }
    
    @Override
    public void loadListNotificationToView(Context context, View view,TextView textView)
    {
	this.textView=textView;
        Database db=new Database(context);        
	ListView listView=(ListView)view;
	ArrayList<android.itv.model.Notification> list=db.getNotifications();
	CustomeAdapterForListNotifications adapter=new CustomeAdapterForListNotifications(list, context);
	listView.setAdapter(adapter);
	
    }
   

    @Override
    public void saveNotificationDB(android.itv.model.Notification notify)
    {
	Database db=new Database(context);
	db.saveNotification(notify);
    }

    @Override
    public ArrayList<android.itv.model.Notification> getNotificationsFromDB(String time)
    {
	// TODO Auto-generated method stub
	return null;
    }

    @Override
    public ArrayList<android.itv.model.Notification> getNotificationsFromDB()
    {
	ArrayList<android.itv.model.Notification> listNotification=new ArrayList<android.itv.model.Notification>();
	Database db=new Database(context);
	listNotification=db.getNotifications();
	return listNotification;
    }
    
    @Override
    public void setNotification(Context context,AlarmManager al, long atTime,String content)
    {
	
	Intent intent=new Intent(context, NotificationBar.class);
	intent.putExtra("NOTIFICATION", content);
	PendingIntent pItent=PendingIntent.getBroadcast(context, 0, intent, 0);	
	AlarmManager alarmM=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
	alarmM.set(AlarmManager.RTC_WAKEUP,atTime, pItent);	
    }
    
    private class CustomeAdapterForListNotifications extends BaseAdapter
    {
	ArrayList<android.itv.model.Notification> listNotification=new ArrayList<android.itv.model.Notification>();
	Context context;
	LayoutInflater inflater;
	 
	public CustomeAdapterForListNotifications(ArrayList<android.itv.model.Notification> listNotification,Context context)
        {
	    this.context=context;
	    this.listNotification=listNotification;
	    inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
		
	@Override
        public int getCount()
        {
	    return listNotification.size();
        }

	@Override
        public Object getItem(int arg0)
        {
	    return listNotification.get(arg0);
        }

	@Override
        public long getItemId(int arg0)
        {
	    return arg0;
        }

	@Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
	    View view=convertView;
	    TextView txtTime,txtDescription;
	    
	    Button btnDelete;
	    
	    if(view==null)
	    {
		view=inflater.inflate(R.layout.customviewnotification, null);
	    }
	    txtTime=(TextView)view.findViewById(R.id.txtTime);
	    txtDescription=(TextView)view.findViewById(R.id.txtDescription);
	    btnDelete=(Button)view.findViewById(R.id.btnDelete);
	    btnDelete.setOnClickListener(new OnClickListener()
	    {
	        
	        @Override
	        public void onClick(View v)
	        {
	    		
	            Database db=new Database(context);
	            db.deleteNotifications(listNotification.get(position).getContent());
	            listNotification.remove(position);
	            notifyDataSetChanged();
	            
	            Intent intent=new Intent(context, NotificationBar.class);
	    	    PendingIntent pItent=PendingIntent.getBroadcast(context, 0, intent, 0);	
	    	    AlarmManager alarmM=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
	    	    alarmM.cancel(pItent);
	            //cap nhat lai danh sach tong so nhac nho
	            int cout=db.countNotifications();
	            String s=context.getResources().getString(R.string.txttotalnotifications);
	            textView.setText(s+" "+cout);
	        }
	    });
	    
	    
	    txtTime.setText(listNotification.get(position).getTime());
	    txtDescription.setText(listNotification.get(position).getContent());	    
	    
	    return view;
        }
	
    }

  
    
   

		
    
  
    
}
